package edu.hhu.basic.dao;
import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

public class BasicDao {
    //根据所选的井号查询
	public Page<Record> Column(String tableName, int page, String para) {
		Page<Record> list = null;
		if ("choice".equals(tableName)) {
			list = Db.use("mysql").paginate(page, 20, "select *",
					"from cjsctjtemp");
		} else {
			System.out.println("haha");
			String[] strPara = para.split(",");
			String sql = "from " + tableName + " where wellname in(";
			for (int i = 0; i < strPara.length; i++) {
				sql += "?";
				if (i < strPara.length - 1) {
					sql += ",";
				}
			}
			sql += ")";
			System.out.println(sql);
			list = Db.use("mysql").paginate(page, 20, "select *", sql, strPara);
		}
		return list;
	}
//基础查询
	public List<Record> BasicQuery(String para,String table){
		String[] split = para.split(",");
		//System.out.println("hheh"+split[0]);
		System.out.println(split.length);
		for (int i = 0; i < split.length; i++) {
			if(" ".equals(split[i])){
			split[i] = "%";
			}else{
			split[i] += "%";
			}
		}
		System.out.println(table);
		List<Record> list = null;
		if ("cjkj".equals(table)) {
			String sql = "select * from " + " " + table + " "
					+ "where 1=1 and wellname like ? and wellgroupcode like ? and blockcode like ?";
			System.out.println(sql);
			list = Db.use("mysql").find(sql, split);
			// Page<Record> page = Db.use("mysql").paginate(1, 20, "select *",
			// "sql", split);
		}
		if ("请选择".equals(table)) {
			String sql = "select * from " + " " + "cjsctjtemp" + " "
					+ "where 1=1 and wellname like ?";
			System.out.println(sql);
			list = Db.use("mysql").find(sql, split);
			// Page<Record> page = Db.use("mysql").paginate(1, 20, "select *",
			// "sql", split);
		} else if ("cjjbxx".equals(table)) {
			String sql = "select * from " + " " + table + " "
					+ "where 1=1 and wellname like ?";
			System.out.println(sql);
			list = Db.use("mysql").find(sql, split);
		} else if ("cjmcxx".equals(table)) {
			String sql = "select * from " + " " + table + " "
					+ "where 1=1 and wellname like ? and Lithologycode like ? and layered like ?";
			System.out.println(sql);
			list = Db.use("mysql").find(sql, split);
		} else if ("cjsjxx".equals(table)) {
			String sql = "select * from " + " " + table + " "
					+ "where 1=1 and wellname like ? and NCS like ?";
			System.out.println(sql);
			list = Db.use("mysql").find(sql, split);
		} else if ("cjdwxf".equals(table)) {
			String sql = "select * from " + " " + table + " "
					+ "where 1=1 and wellname like ? and NCS like ?";
			System.out.println(sql);
			list = Db.use("mysql").find(sql, split);
		} else if ("cjzfxx".equals(table)) {
			String sql = "select * from " + " " + table + " "
					+ "where 1=1 and wellname like ? and ";
			System.out.println(sql);
			list = Db.use("mysql").find(sql, split);
		} else if ("cjscjt".equals(table)) {
			String sql = "select * from" + " " + table
					+ " where 1=1 and TCRQ like ? and JQRQ like ?";
			list = Db.use("mysql").find(sql, split);
		} else if ("cjscrsctemp".equals(table)) {
			String sql = "select * from " + " " + table + " "
					+ "where 1=1 and wellname like ?";
			System.out.println(sql);
			list = Db.use("mysql").find(sql, split);
		} else if ("cjsctjtemp".equals(table)) {
			String sql = "select * from " + " " + table + " "
					+ "where 1=1 and wellname like ?";
			System.out.println(sql);
			list = Db.use("mysql").find(sql, split);
		} else if ("cjgbyxpz".equals(table)) {
			String sql = "select * from " + " " + table + " "
					+ "where 1=1 and wellname like ?";
			System.out.println(sql);
			list = Db.use("mysql").find(sql, split);
		} else if ("cjgblxpz".equals(table)) {
			String sql = "select * from " + " " + table + " "
					+ "where 1=1 and wellname like ?";
			System.out.println(sql);
			list = Db.use("mysql").find(sql, split);
		} else if ("cjgbxx".equals(table)) {
			String sql = "select * from " + " " + table + " "
					+ "where 1=1 and wellname like ?";
			System.out.println(sql);
			list = Db.use("mysql").find(sql, split);
		} else if ("mcyxpz".equals(table)) {
			String sql = "select * from " + " " + table + " "
					+ "where 1=1 and wellname like ?";
			System.out.println(sql);
			list = Db.use("mysql").find(sql, split);
		} else if ("cjbbtjtrue".equals(table)) {
			String sql = "select * from " + " " + table + " "
					+ "where 1=1 and wellname like ?";
			System.out.println(sql);
			list = Db.use("mysql").find(sql, split);
		} else if ("cjbbrsctrue".equals(table)) {
			String sql = "select * from " + " " + table + " "
					+ "where 1=1 and wellname like ?";
			System.out.println(sql);
			list = Db.use("mysql").find(sql, split);

		}
		return list;
	}
	public List<Record> AdvanceQuery(String para,String table){
		String[] split = para.split(",");
	      String sql = "select * from "+" "+table+" "+"where"+" "+split[0]+""+split[1]+""+"'"+split[2]+"'";
	      System.out.println(sql);
	      List<Record> list = Db.find(sql);
	      if(list==null){
	    	  list=new ArrayList();
	      }
		return list;
		
	}
//高级查询
	public List<Record> AdvanceQuery1(String para){
		  String[] split = para.split(",");
	      System.out.println(para);
	      int len=split.length;
	      String sql=null;
	      List<Record> list=null;
	      if(len==4){
	    	   sql = "select * from"+" "+split[0]+" "+"where"+" "+split[1]+split[2]+"?";
	    	   list=Db.use("mysql").find(sql, split[3]);
	    	   System.out.println(sql);
	      }else if(len==8){
	    	   sql = "select * from"+" "+split[0]+" "+" a inner join"+" "+split[4]+" "+"b on a.wellcode=b.wellcode where a."+split[1]+split[2]+"?"+"and b."+split[5]+split[6]+"?"; 
	    	   list=Db.use("mysql").find(sql, split[3],split[7]);
	    	   System.out.println(sql);
	      }
	     
      if(list==null){
  	  list=new ArrayList();
    }
	return list;
	
}
	
	
}
